package com.yint.http.request;

import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

import com.yint.http.Method;
import com.yint.http.api.Body;

public class Request
{
	private Method method;
	private Body body;
	private String urlStr;
	private int connectTimeout = 5000, readTimeout = -1;
	private SSLSocketFactory factory;
	private Map< String,String > heads = new HashMap< String,String >();

	private Request()
	{

	}
	
	public Method getMethod()
	{
		return method;
	}

	public Body getBody()
	{
		return body;
	}

	public String getUrlStr()
	{
		return urlStr;
	}

	public Map< String, String > getHeads()
	{
		return heads;
	}

	public void addHeader( String key, String value )
	{
		heads.put( key, value );
	}
	
	public SSLSocketFactory getSSLSocketFactory()
	{
		return factory;
	}
	
	public void setSSLSocketFactory(SSLSocketFactory factory)
	{
		this.factory=factory;
	}

	public int getConnectTimeout()
	{
		return connectTimeout;
	}

	public int getReadTimeout()
	{
		return readTimeout;
	}
	
	public static class Builder
	{
		private Method method=Method.GET;
		private String url;
		private Body body;
		private int connectTimeout = -1, readTimeout = -1;
		private Map< String,String > heads = new HashMap< String,String >();
		private SSLSocketFactory factory;
		
		public Builder addHeader( String key, String value )
		{
			heads.put( key, value );
			return this;
		}

		public Builder url( String url )
		{
			this.url=url;
			return this;
		}

		public Builder post( Body body )
		{
			this.body=body;
			this.method=Method.POST;
			return this;
		}
		
		public Builder setConnectTimeout( int time )
		{
			this.connectTimeout=time;
			return this;
		}
		
		public Builder setReadTimeout( int time )
		{
			this.readTimeout=time;
			return this;
		}

		public Builder setSSLSocketFactory(SSLSocketFactory factory )
		{
			this.factory=factory;
			return this;
		}
		
		public Request build()
		{
			if(url==null || url.length()==0)
			{
				throw new IllegalArgumentException("url is null");
			}
			
			Request request=new Request();
			request.method=method;
			request.urlStr=url;
			request.body=body;
			request.heads=heads;
			request.connectTimeout=connectTimeout;
			request.readTimeout=readTimeout;
			request.factory=factory;
			return request;
		}
	}
}
